//
//  FormatUtils.m
//  SZYH2
//
//  Created by WT on 13-6-10.
//  Copyright (c) 2013年 JMU. All rights reserved.
//

#import "FormatUtil.h"

@implementation FormatUtil

+ (NSDate *)dateFromString:(NSString *)dateString withFormat:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.timeZone = [NSTimeZone systemTimeZone];
    [formatter setDateFormat:format];
    
    NSDate *date = [formatter dateFromString:dateString];
    return date;
}

+ (NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format
{
    if (date == nil) {
        return @"";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.timeZone = [NSTimeZone systemTimeZone];
    [formatter setDateFormat:format];
    
    return [formatter stringFromDate:date];
}

+ (NSString *)stringFromSize:(float)size
{
    float value = size;
    
    if (size < 1000) {
        value = size;
        return [NSString stringWithFormat:@"%.2f", value];
    } else if (size >= 1000 && size < 1000*1000) {
        value = size/(1024);
        return [NSString stringWithFormat:@"%.2fK", value];
    } else if (size > 1000*1000 && size < 1024*1024*1024) {
        value = size/(1024*1024);
        return [NSString stringWithFormat:@"%.2fM", value];
    } else {
        value = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.2fG", value];
    }
}


// 修改时间格式，今天内显示为今天，其他日期按照yyyy-MM-dd HH:mm
+ (NSString *)changeDateline:(SInt64)dateline
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateline/1000];;
    // 1.获得年月日
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:date];
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if ([cmp1 day] == [cmp2 day]) {
        formatter.dateFormat = @"今天 HH:mm:ss";
    }
    else if ([cmp1 day] - 1 == [cmp2 day]){
        formatter.dateFormat = @"明天 HH:mm:ss";
    }
    else {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    }
    NSString *time = [formatter stringFromDate:date];
    return time;
}


// 获得某个时间和当前时间隔多少天
+ (NSInteger)comparisonOfDays:(SInt64)iDate
{
    //获得当前时间
    NSDate *now = [NSDate date];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strNow = [dateFormatter stringFromDate:now];
    now = [[self class] dateFromString:strNow withFormat:@"yyyy-MM-dd"];
    
    NSDate *inputDate = [NSDate dateWithTimeIntervalSince1970:iDate/1000];
    NSString *strInputDate = [dateFormatter stringFromDate:inputDate];
    inputDate = [dateFormatter dateFromString:strInputDate];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:inputDate toDate:now options:0];
    return [comps day];
}


// 获得某个时间和当前时间隔多少分钟
+ (NSInteger)comparisonOfMinutes:(SInt64)date
{
    //获得当前时间
    NSDate *now = [NSDate date];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *oldDate = [NSDate dateWithTimeIntervalSince1970:date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitMinute;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:now toDate:oldDate options:0];
    return [comps minute];
}

// 计算间隔秒数
+ (NSInteger)comparisonOfSeconds:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitSecond;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date toDate:[NSDate date] options:0];
    return [comps second];
}

// 计算间隔毫秒数
+ (NSTimeInterval)comparisonOfMilliseconds:(NSDate *)date
{
    NSDate *nowDate = [NSDate date];
    NSTimeInterval startInterval = [date timeIntervalSince1970];
    NSTimeInterval endInterval = [nowDate timeIntervalSince1970];
    return endInterval - startInterval;
}


// 比较两个时间的大小: 1 > 2 返回NSOrderedAscending
+ (NSComparisonResult)compareDate:(SInt64)iTime1 andIntTime:(SInt64)iTime2
{
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:iTime1/1000];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:iTime2/1000];
    return [self compareDate:date1 andDate:date2];
}

+ (NSComparisonResult)compareDate:(NSString *)strTime1 andtime:(NSString *)strTime2
{
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *date1 = [dateformater dateFromString:strTime1];
    NSDate *date2 = [dateformater dateFromString:strTime2];
    return [self compareDate:date1 andDate:date2];
}

+ (NSComparisonResult)compareDate:(NSDate *)date1 andDate:(NSDate *)date2
{
    return [date1 compare:date2];
}

@end
